import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * @author lys
 * 删除书籍
 */
public class DeleteBook extends JFrame {
    private static DeleteBook instance;

    // 标签
    private JLabel jLabel = new JLabel("删除图书");
    private JLabel jLabel2 = new JLabel("请输入书号：");
    private JLabel jLabel3 = new JLabel("书名：");
    private JLabel jLabel4 = new JLabel();
    private JLabel jLabel5 = new JLabel("作者：");
    private JLabel jLabel6 = new JLabel();
    private JLabel jLabel7 = new JLabel("状态：");
    private JLabel jLabel8 = new JLabel();
    private JLabel jLabel9 = new JLabel("书号：");
    private JLabel jLabel10 = new JLabel();

    // 文本框
    private JTextField field = new JTextField(20);
    // 大小
    // private Dimension dimension = new Dimension(220, 20);
    // 按钮
    private JButton button = new JButton("检索");
    private JButton button2 = new JButton("确认删除");
    private JButton button3 = new JButton("返回");

    // 面板
    private JPanel jPanel = new JPanel();
    // 字体
    private Font font = new Font("宋体", Font.BOLD, 60);
    private Font font2 = new Font("宋体", Font.BOLD, 30);
    private Font font3 = new Font("宋体", Font.BOLD, 26);

    private DeleteBook() {
        // 改变背景图片
        Icon i = new ImageIcon(DeleteBook.class.getResource("img/login2.jpg"));
        JLabel Label = new JLabel(i);
        Label.setBounds(0, 0, 1200, 800);

        // 标签
        jLabel.setFont(font);
        jLabel.setBounds(280, 35, 800, 100);
        jLabel.setForeground(Color.white);

        jLabel2.setFont(font2);
        jLabel2.setBounds(80, 164, 250, 30);
        jLabel2.setForeground(Color.white);

        // 文本框
        field.setFont(font2);
        field.setBackground(Color.white);
        field.setBounds(280, 164, 250, 30);
        field.setForeground(Color.white);
        field.setOpaque(false);

        // 按钮
        button.setFont(font2);
        button.setBounds(580, 160, 100, 40);
        button.setForeground(Color.white);
        button.setBackground(Color.white);
        button.setOpaque(false);

        // 面板
        jPanel.setBounds(70, 220, 620, 450);
        jPanel.setBackground(Color.white);
        jPanel.setLayout(null);

        // 面板上标签及按钮
        jLabel3.setFont(font2);
        jLabel4.setFont(font2);
        jLabel5.setFont(font2);
        jLabel6.setFont(font2);
        jLabel7.setFont(font2);
        jLabel8.setFont(font2);
        jLabel9.setFont(font2);
        jLabel10.setFont(font2);

        button2.setFont(font3);
        button3.setFont(font3);

        jLabel3.setForeground(Color.black);
        jLabel4.setForeground(Color.black);
        jLabel5.setForeground(Color.black);
        jLabel6.setForeground(Color.black);
        jLabel7.setForeground(Color.black);
        jLabel8.setForeground(Color.black);
        jLabel9.setForeground(Color.black);
        jLabel10.setForeground(Color.black);

        button2.setForeground(Color.black);
        button3.setForeground(Color.black);

        jLabel9.setBounds(100, 20, 100, 50);
        jLabel10.setBounds(200, 20, 400, 50);
        jLabel3.setBounds(100, 90, 100, 50);
        jLabel4.setBounds(200, 90, 400, 50);
        jLabel5.setBounds(100, 160, 100, 50);
        jLabel6.setBounds(200, 160, 400, 50);
        jLabel7.setBounds(100, 230, 100, 50);
        jLabel8.setBounds(200, 230, 400, 50);

        button2.setBounds(250, 310, 150, 50);
        button3.setBounds(500, 630, 90, 50);
        button2.setBackground(Color.white);
        button2.setOpaque(false);
        button3.setBackground(Color.white);
        button3.setOpaque(false);

        jPanel.add(jLabel3);
        jPanel.add(jLabel4);
        jPanel.add(jLabel5);
        jPanel.add(jLabel6);
        jPanel.add(jLabel7);
        jPanel.add(jLabel8);
        jPanel.add(jLabel9);
        jPanel.add(jLabel10);
        jPanel.add(button2);
        //jPanel.add(button3);
        jPanel.setVisible(false);

        jPanel.setOpaque(false);

        add(jLabel);
        add(jLabel2);
        add(field);
        add(button);
        add(jPanel);
        add(button3);
        add(Label);

        setSize(800,800);
        setLocationRelativeTo(null);
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        button.addActionListener(
                new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        jPanel.setVisible(true);
                        Book book = BorrowAndReturn.returnSearch(field.getText());
                        if(book != null){
                            jLabel10.setText(String.valueOf(book.getBookId()));
                            jLabel4.setText(book.getBookName());
                            jLabel6.setText(book.getWriter());
                            if(book.isBorrowed()){
                                jLabel8.setText("已借");
                            }else{
                                jLabel8.setText("在馆");
                            }
                        }
                    }
                }
        );

        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                deleteBook(field.getText());
                ManageBook.getInstance().showBooks();
                setVisible(false);
                removeInstance();
                AdminInterface.getInstance().admin.setVisible(true);
            }
        });

        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                AdminInterface.getInstance().admin.setVisible(true);
            }
        });

        button.setFocusPainted(false);
        button2.setFocusPainted(false);
        button3.setFocusPainted(false);
    }

    public  static  DeleteBook getInstance() {
        if(instance==null) instance=new DeleteBook();
        return instance;
    }

    public static void main(String[] args) {
        DeleteBook.getInstance();
    }
    /**
     * 删除书籍
     * @author zj
     */
    public void deleteBook(String bookId){
        String sql = "select * from borrowinfo where bookid = ? and isreturned = 0";
        try{
            PreparedStatement preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
            preSql.setString(1, bookId);
            ResultSet rs = preSql.executeQuery();
            if(rs.next()){
                JOptionPane.showMessageDialog(null, "用户尚未归还该书，无法删除！");
                return;
            }else{
                sql = "delete from borrowinfo where bookid = ?";
                preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
                preSql.setString(1, bookId);
                preSql.executeUpdate();
                sql = "delete from book where bookid = ?";
                preSql = DatabaseConnection.getInstance().getCon().prepareStatement(sql);
                preSql.setString(1, bookId);
                preSql.executeUpdate();
                JOptionPane.showMessageDialog(null, "成功删除该书！");
            }
        }catch (Exception e){
            System.out.println(e);
        }
    }
    public static void removeInstance(){
        instance = null;
    }
}
